package com.leetcode.string;

/**
 * @author Dennis Li
 * @date 2020/9/14 11:07
 */
public class LongestCommonPrefix_14 {

    public static void main(String[] args) {
        LongestCommonPrefix_14 test = new LongestCommonPrefix_14();
        String[] strs = {"flower","flow","flight"};
        String prefix = test.longestCommonPrefix(strs);

        System.out.println(prefix);
    }

    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) return "";
        String first = strs[0];
        int prefix = first.length();
        for (String str : strs) {
            prefix = Math.min(str.length(), prefix);
            for (int i = 0; i < prefix; i++) {
                if (str.charAt(i) != first.charAt(i)) {
                    if (i == 0) return "";
                    prefix = i;
                    break;
                }
            }
        }
        return first.substring(0, prefix);
    }

}
